<%@ WebHandler Language="C#" Class="VerifyCode" %>

using System;
using System.Web;
using System.Drawing;

public class VerifyCode : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string checkCode = context.Request.QueryString["checkCode"];
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(Convert.ToInt32(Math.Ceiling((decimal)(checkCode.Length * 14))), 24);
        Graphics g = Graphics.FromImage(image);

        try
        {
            Random random = new Random();
            g.Clear(Color.Black);

            Font font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, font, new SolidBrush(Color.White), 2, 2);

            g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            context.Response.ClearContent();
            context.Response.ContentType = "image/Gif";
            context.Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}